package com.google.code.netbuilder.dal.providersimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.google.code.netbuilder.dal.entities.Port;
import com.google.code.netbuilder.dal.providers.IPortProvider;
import com.google.code.netbuilder.dal.services.JDBCServise;

public class PortProviderJDBC implements IPortProvider {

	@Override
	public void save(Port entity) throws RuntimeException {
		String query = "INSERT INTO port(id, name)" +
				" VALUES (?,?);";
		Connection con = null;
		PreparedStatement pst = null;
		try
		{
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			int rs=-1;
			pst = con.prepareStatement(query);
			pst.setString(1, entity.getId().toString());
			pst.setString(2, entity.getName());
			rs = pst.executeUpdate();
			if (rs<=0) throw new SQLException("Port didn't INSERT");
		}
		catch(SQLException e)
		{
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}
	}

	@Override
	public void update(Port entity) throws RuntimeException {
		String query = "UPDATE port SET name=? WHERE id=?;";
				
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setString(1, entity.getName());			
			pst.setInt(2, entity.getId());
			int rs = pst.executeUpdate();		
			if (rs <= 0) throw new SQLException("Port didn't update");
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}
	}

	@Override
	public void delete(Port entity) throws RuntimeException {
		String query = "DELETE FROM port WHERE id=?;";
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JDBCServise.getConnection();
			if (con == null) throw new SQLException("No Data Base Connection");
			pst = con.prepareStatement(query);
			pst.setInt(1, entity.getId());		
			int rs = pst.executeUpdate();	
			if (rs <= 0) throw new SQLException("Port didn't DELETE");
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}		
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
				}
		}
	}

	@Override
	public List findAll() throws RuntimeException {
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Port> ports = null;
		
		try {
			con = JDBCServise.getConnection();
        
			pst = con.prepareStatement("Select * From port");
			rs = pst.executeQuery();
        
			ports = new ArrayList<Port>();
        
			while (rs.next()) {
				Port port = new Port();
				port.setId(rs.getInt("id"));
				port.setName(rs.getString("name"));
				ports.add(port);
			}
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}	
		
        return ports;		
	}

	@Override
	public Port findByID(Integer id) throws RuntimeException {
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		Port port = null;
		try {
			con = JDBCServise.getConnection();
	     
			if (con == null) throw new SQLException("No Data Base Connection");          
			pst = con.prepareStatement("SELECT * FROM port where id ="
        								+ id.toString());
			rs = pst.executeQuery();
       
			if (!rs.first()) throw new SQLException("Port not found");
			
			port = new Port();
        
			port.setId(rs.getInt("id"));
			port.setName(rs.getString("name"));
			
		} catch (SQLException exc)
		{
			throw new RuntimeException(exc);
		}
		finally
		{
			try {
				if (con != null) con.close();
				if (pst != null) pst.close();
				if (rs != null) rs.close();
			} catch (SQLException exc) {
				throw new RuntimeException(exc);
				}
		}
		return port;
	}

}
